<?php
/**
 * Project: Recipe
 * File: FoodCollocationAction.class.php
 * Author: Xnfy520@gmail.com
 * Date: 12-4-19
 * Time: 上午10:31
 */

class FoodCollocationAction extends CommonAction{


	function index(){

		import("ORG.Util.Page");

		$foodcategory = D('FoodCategory');

		$foodsection = M('FoodSection');
		$foodsectioninfo = $foodsection->field('id,name')->select();
		$this->assign('foodsectioninfo', $foodsectioninfo);

		if(isset($_GET['sid'])){
			$foodcategoryinfo = $foodcategory->field('id,name')->where('foodsectionid='.$_GET['sid'])->select();
			$this->assign('foodcategoryinfo', $foodcategoryinfo);
		}

		$foodarticle = M('FoodArticle');

		$foodcollocation = D('FoodCollocation');

		if(isset($_POST['search'])){

			if(!empty($_POST['key'])){
				switch($_POST['search']){
					case 1 :
						$datas['id'] = $_POST['key'];
						break;
				}
				if(isset($_GET['sid'])){
					$datas['sectionid'] = $_GET['sid'];
				}
				$total = $foodcollocation->where($datas)->count();

				$Page = new Page($total, C('PAGENUM'));
				$show = $Page->show();
				$list = $foodcollocation->relation(true)->where($datas)->limit($Page->firstRow.','.$Page->listRows)->order('id desc')->select();
				foreach($list as $key=>$value){
					$list[$key]['orgarticle'] = $foodarticle->field('name,image')->find($value['articleid']);
					$list[$key]['orgname'] = $list[$key]['orgarticle']['name'];
				}
			}else{
				$this->error('搜索内容不能为空', __APP__.'/FoodCategory/index');
			}

		}else{

			if(!isset($_GET['sid']) && !isset($_GET['cid']) && !isset($_GET['id'])){
				$total = $foodcollocation->count();
				$Page = new Page($total, C('PAGENUM'));// 实例化分页类 传入总记录数和每页显示的记录数
				$show = $Page->show();// 分页显示输出
				$list = $foodcollocation->relation(true)->limit($Page->firstRow.','.$Page->listRows)->order('id desc')->select();
				foreach($list as $key=>$value){
					$list[$key]['orgarticle'] = $foodarticle->field('name,image')->find($value['articleid']);
					$list[$key]['orgname'] = $list[$key]['orgarticle']['name'];
				}

			}elseif(isset($_GET['sid']) && isset($_GET['cid']) && isset($_GET['id'])){

/*				$datas['sectionid'] = $_GET['sid'];
				$datas['categoryid'] = $_GET['cid'];
				$datas['articleid'] = $_GET['id'];*/

				$datas['_query'] = 'articleid='.$_GET['id'].'&destarticleid='.$_GET['id'].'&_logic=or';

				$total = $foodcollocation->where($datas)->count();
				$Page = new Page($total, C('PAGENUM'));// 实例化分页类 传入总记录数和每页显示的记录数
				$show = $Page->show();// 分页显示输出
				$list = $foodcollocation->relation(true)->where($datas)->limit($Page->firstRow.','.$Page->listRows)->order('id desc')->select();
				foreach($list as $key=>$value){
					$list[$key]['orgarticle'] = $foodarticle->field('name,image')->find($value['articleid']);
				}

				$data['sectionid'] = $_GET['sid'];
				$data['categoryid'] = $_GET['cid'];
				$data['articleid'] = $_GET['id'];
				$collocationlist = $foodcollocation->where($data)->order('id desc')->select();

				if(!empty($collocationlist)){
					$orgname = $foodarticle->field('name')->getById($_GET['id']);
				}

				$this->assign('orgname', $orgname);

			}elseif(isset($_GET['sid']) && isset($_GET['cid'])){

				$datas['sectionid|destsectionid'] = $_GET['sid'];
				$datas['categoryid|destcategoryid'] = $_GET['cid'];

				$total = $foodcollocation->where($datas)->count();

				$Page = new Page($total, C('PAGENUM'));// 实例化分页类 传入总记录数和每页显示的记录数
				$show = $Page->show();// 分页显示输出
				$list = $foodcollocation->relation(true)->where($datas)->limit($Page->firstRow.','.$Page->listRows)->order('id desc')->select();
				foreach($list as $key=>$value){
					$list[$key]['orgarticle'] = $foodarticle->field('name,image')->find($value['articleid']);
				}

			}elseif(isset($_GET['sid'])){
				$total = $foodcollocation->where('sectionid='.$_GET['sid'].' OR destsectionid='.$_GET['sid'])->count();
				$Page = new Page($total, C('PAGENUM'));// 实例化分页类 传入总记录数和每页显示的记录数
				$show = $Page->show();// 分页显示输出
				$list = $foodcollocation->relation(true)->where('sectionid='.$_GET['sid'].' OR destsectionid='.$_GET['sid'])->limit($Page->firstRow.','.$Page->listRows)->order('id desc')->select();
				foreach($list as $key=>$value){
					$list[$key]['orgarticle'] = $foodarticle->field('name,image')->find($value['articleid']);
				}

			}

		}

		$this->assign('list',$list);// 赋值数据集

		$this->assign('show',$show);// 赋值分页输出

		$this->display('index');
	}

	/**
	 * 指向添加食材搭配界面
	 */
	function add(){
		$foodsection = M('FoodSection');
		$foodsectioninfo = $foodsection->field('id,name')->select();
		$this->assign('data', $foodsectioninfo);
		$this->display();
	}

	/**
	 * 添加食材搭配
	 */
	function insert(){
		$foodcollocation = M('FoodCollocation');

		if($data = $foodcollocation->create()){
			dump($data);
			$aid = $data['articleid'];
			$sid = $data['sectionid'];
			$cid = $data['categoryid'];
		//	unset($data['articleid']);

				for($i=0; $i<count($data['destarticleid']); $i++){
					if($data['destarticleid'][$i]==0){
						continue;
					}
					$datas['articleid']=$aid;
					$datas['sectionid']=$sid;
					$datas['categoryid']=$cid;
					$datas['destarticleid']=$data['destarticleid'][$i];
					$datas['destsectionid']=$data['destsectionid'][$i];
					$datas['destcategoryid']=$data['destcategoryid'][$i];
					$datas['goodorbad']=$data['goodorbad'][$i];
					$datas['description']=$data['description'][$i];
				//	dump($datas);
					if($foodcollocation->add($datas)){
						$this->success('添加搭配成功',__APP__.'/FoodCollocation/index');
					}else{
						$this->error('添加搭配失败',__APP__.'/FoodCollocation/add');
					}
				}

		}else{
			$this->error($foodcollocation->getError(), __APP__.'/FoodCollocation/add');
		}
	}

	/**
	 * 指向修改食材搭配界面
	 */
	function edit(){

		if(isset($_GET['id']) && !empty($_GET['id'])){

			$foodsection = M('FoodSection');
			$foodsectioninfo = $foodsection->field('id,name')->select();
			$this->assign('sectiondata', $foodsectioninfo);

			$foodcategory = M('FoodCategory');
			$foodcategoryinfo = $foodcategory->field('id,name')->where('foodsectionid='.$_GET['sid'])->select();
			$this->assign('categorydata', $foodcategoryinfo);

			$foodarticle = M('FoodArticle');
			$foodarticleinfo = $foodarticle->field('id,name')->where('sectionid='.$_GET['sid'].' AND categoryid='.$_GET['cid'])->select();
			$this->assign('articledata', $foodarticleinfo);

			$orgname = $foodarticle->where('id='.$_GET['id'])->getField('image');
			$this->assign('orgname', $orgname);

			$foodcollocation = D('FoodCollocation');

			$datas['sectionid'] = $_GET['sid'];
			$datas['categoryid'] = $_GET['cid'];
			$datas['articleid'] = $_GET['id'];

		//	$datas['_query'] = 'articleid='.$_GET['id'].'&destarticleid='.$_GET['id'].'&_logic=or';

			$list = $foodcollocation->relation(true)->where($datas)->order('id desc')->select();

			foreach($list as $key=>$value){
			//	dump($list[$key]['destarticle']['categoryid']);
				$list[$key]['destsectionname'] = $foodsection->where('id='.$list[$key]['destsectionid'])->getField('name');
				$list[$key]['destcategoryname'] = $foodcategory->where('id='.$list[$key]['destcategoryid'])->getField('name');
			//	$list[$key]['destcategoryname'] = $foodcategory->where('id='.$list[$key]['destarticle']['categoryid'])->getField('name');
			}

			$this->assign('list',$list);

			$this->display();
		}else{
			$this->error('异常操作');
		}

	}

	/**
	 * 修改食材搭配
	 */
	function update(){
		if(isset($_POST['id']) && !empty($_POST['id'])){

			$foodcollocation = M('FoodCollocation');

			if($data = $foodcollocation->create()){
				$aid = $data['articleid'];
				$sid = $data['sectionid'];
				$cid = $data['categoryid'];
			//	unset($data['articleid']);

				for($j=0; $j<count($data['id']); $j++){
					$foodcollocation->delete($data['id'][$j]);
				}

				for($i=0; $i<count($data['destarticleid']); $i++){

					if($data['destarticleid'][$i]==0){
						continue;
					}

					$datas['articleid']=$aid;
					$datas['sectionid']=$sid;
					$datas['categoryid']=$cid;

					$datas['destarticleid']=$data['destarticleid'][$i];
					$datas['destsectionid']=$data['destsectionid'][$i];
					$datas['destcategoryid']=$data['destcategoryid'][$i];

					$datas['goodorbad']=$data['goodorbad'][$i];
					$datas['description']=$data['description'][$i];

					if($foodcollocation->add($datas)){
						$this->success('修改搭配成功',__APP__.'/FoodCollocation/index');
					}else{
						$this->error('修改搭配失败');
					}
				}

			}else{
				$this->error($foodcollocation->getError());
			}

		}else{
			$this->error('异常操作');
		}
	}

	/**
	 * 删除食材搭配
	 */
	function del(){
		if(!empty($_POST['actions'])){
			$foodcollocation = M('FoodCollocation');
			foreach($_POST['actions'] as $key=>$value){
					$num = $foodcollocation->delete($value);
			}
			if($num>0){
				$this->success('删除食材搭配成功', __APP__.'/FoodCollocation/index');
			}else{
				$this->error('删除食材搭配失败', __APP__.'/FoodCollocation/index');
			}
		}else{
			$this->error('请先选择食材搭配', __APP__.'/FoodCollocateion/index');
		}
	}

	function returncategorys(){
		$foodcategory = M('FoodCategory');
		$list = $foodcategory->field('id,name,image')->where('foodsectionid='.$_POST['id'])->select();
		$data = array();
		foreach($list as $key=>$value){
			$data[] = $value;
		}
		echo json_encode($data);
	}

	function returnarticles(){
		$foodarticle = M('FoodArticle');
//		$foodcollocation = M('FoodCollocation');
		$datas['sectionid'] = $_POST['sid'];
		$datas['categoryid'] = $_POST['cid'];

/*		$abc = '';
		$articleid = $foodcollocation->field('id')->where($datas)->select();
		for($i=0;$i<count($articleid);$i++){
			$abc[]=$articleid[$i]['id'];
		}
		$abc=implode(',',$abc);*/

		if(isset($_POST['id'])){
			$datas['id'] = $_POST['id'];
			$data = $foodarticle->field('id,name,image')->find($datas['id']);
		}else{
			if(isset($_POST['oaid'])){
				$datas['id']  = array('not in',$_POST['oaid']);
			}
			$list = $foodarticle->field('id,name,image')->where($datas)->select();
			$data = array();
			foreach($list as $key=>$value){
				$data[] = $value;
			}
		}
		echo json_encode($data);
	}

}